import logging

import requests
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
import binascii

# 代理
proxies = {
    'http': '127.0.0.1:7890',
    'https': '127.0.0.1:7890'
}

# 定义日志
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s: %(message)s')


def ase_encrypt(data):
    """
    AES 加密数据
    :param data:
    :return:
    """
    key = b'qwertyuioplkjhgf'  # 与脚本相同的 key
    data = 'https://www.instagram.com/p/DFW9hH7qF9C/'.encode('utf-8')

    cipher = AES.new(key, AES.MODE_ECB)
    encrypted = cipher.encrypt(pad(data, 16))
    hex_str = binascii.hexlify(encrypted).decode()
    return hex_str


def api_ajax(url):
    """
    根据目标站点链接，站点名称，返回解析的资源
    :param url: 站点链接
    :return:
    """
    # 加密处理url
    ase_url = ase_encrypt(url)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36',
        'Referer': 'https://reelsave.app/',
        'Url': ase_url
    }
    result = None
    logging.info('开始请求url, url: %s', url)
    try:
        # 发出请求
        response = requests.request('GET', url='https://api.videodropper.app/allinone', headers=headers,
                                    proxies=proxies)
        # 响应成功
        if response.status_code == 200:
            # 返回的json数据
            result = response.json()
            return result
        logging.error('请求状态码错误, code: %s', response.status_code)
    except requests.RequestException as e:
        logging.error('请求链接报错，url: %s', url, exc_info=True)


if __name__ == '__main__':
    res = api_ajax('https://www.instagram.com/p/DJJG88HzVQz/?igsh=MWFwYXFkNG00bmV0eg==')
    print(res)
